package edu.nf.api.controller.alipay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import edu.nf.api.controller.BaseController;
import edu.nf.api.controller.alipay.config.AlipayConfig;
import edu.nf.util.alipay.OrderUtile;
import edu.nf.waimai.dao.order.OrderDAO;
import edu.nf.waimai.dao.order.impl.OrderDAOImpl;
import edu.nf.waimai.model.order.Order;
import org.nf.web.annotation.Model;
import org.nf.web.annotation.RequestMapping;
import org.nf.web.annotation.RequestParam;
import org.nf.web.servlet.View;
import org.nf.web.servlet.view.JsonView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 *
 * @Date 2023-06-26
 * @Author qiu
 *
 * 支付宝支付
 */
public class PayController extends BaseController {

    @RequestMapping("/alipay/pay")
    public void payment(HttpServletResponse resp, HttpSession session) throws IOException {
        // 获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id,
                AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key,
                AlipayConfig.sign_type);

        // 设置请求参数
        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
        // 支付成功后的跳转页面
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        // 支付成功后的回调页面
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

        Order order = (Order)session.getAttribute("order");

        // 商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = OrderUtile.orderNo() ;
        // 付款金额，必填
        String total_amount = String.valueOf(order.totalPrice);
        // 订单名称，必填
        String subject = "WaiMai";
        // 商品描述，可空
        String body = "测试";

        // 该笔订单允许的最晚付款时间，逾期将关闭交易。取值范围：1m～15d。m-分钟，h-小时，d-天，1c-当天（1c-当天的情况下，无论交易何时创建，都在0点关闭）。 该参数数值不接受小数点， 如 1.5h，可转换为 90m。
        String timeout_express = "1c";

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"" + timeout_express + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");


        // 请求
        String result = "";
        try {
            result = alipayClient.pageExecute(alipayRequest).getBody();
        } catch (AlipayApiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        PrintWriter out = resp.getWriter();
        // 输出
        out.println(result);
        out.flush();
        out.close();
        System.out.println(result);
    }

}
